<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){	
	if($_SESSION['onemerchid']){
		$_SESSION['isdh']=1;
	}
	$basic=$this->plugin_setting('basic');
	$return['setting']=$basic;
	$title=$basic['share_title']?:$basic['title'];
	$desc=$basic['share_desc'];
	$logo=$basic['share_image'];
	$return['member']=cfc('core')->get_member($_SESSION['uid']);
	$params=array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'onepluginname'=>'jy_mcaigou');
	$params['parentid']=intval($_RQ['id']);
	$category=pdo_getall('jy_weishop_goods_group',$params,'','id','paixu desc');
	$return['category']=$category;
	$sharelink='jy_mcaigou/mindex//ismerch=1&merchid='.$_SESSION['onemerchid'].'&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$title?:'人人分商城-商城',
			'desc'=>$desc?:'人人分商城！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($logo),
	);
	$return['list']=array();
	$return['pcd']=array();
	$return['params']['pid']=intval($_RQ['pid']);
	$return['time']=time();
	set_html('title',$title);
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'nochecked'=>0,
		'no_showsys'=>0,
		'type <>'=>4,
		'onepluginname'=>'jy_mcaigou',
		'merchid'=>0,
	);
	if($params['merchid']){
		unset($params['no_showsys']);
		unset($params['onepluginname']);
	}
	if($_RQ['cid']){
		$params['ccate']=intval($_RQ['cid']);
		$return['params']['ccate']=intval($_RQ['cid']);
	}	
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	if($_RQ['pid']){
		$params['pcate']=intval($_RQ['pid']);
		$return['params']['pid']=intval($_RQ['pid']);
	}else{
		$return['params']['pid']=0;
	}
	if($_RQ['order']=='isrecommand'){
		$params['saletype likes']=array('1');
		$return['params']['order']='isrecommand';
	}
	if($_RQ['order']=='isnew'){
		$params['saletype likes']=array('2');
		$return['params']['order']='isnew';
	}
	if($_RQ['order']=='ishot'){
		$params['saletype likes']=array('3');
		$return['params']['order']='ishot';
	}
	if($_RQ['priceMin']){
		$params['price >=']=intval($_RQ['priceMin']);
		$return['params']['priceMin']=intval($_RQ['priceMin']);
	}
	if($_RQ['priceMax']){
		$params['price <=']=intval($_RQ['priceMax']);
		$return['params']['priceMax']=intval($_RQ['priceMax']);
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','image','price','active_plugin','active_id','salenum','originalprice','usermaxbuy','unit','api_data');
	$orderby='paixu desc, star desc, id desc ';
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if(empty($list)){
		exi('none');
	}else{
		foreach($list as $k=>&$itemv){
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['jianprice']=$itemv['originalprice']-$itemv['price'];
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//按点赞数排序作品
			$pindex = max(1, 0);
			$psize = 4;
			$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
			foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=$memberinfo['avatar'];
			}
			$itemv['members']=$members;			
			$itemv['merchname']=$myshop['merchname'];
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],),'id');
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
			$itemv['specs_list']=array();
			if($_RQ['is_specs'] && $itemv['optionid']){
				//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specs_list){
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
						}
				}
				//已选规格
				$selectspecs = array();
				$selespename = '';
				$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$itemv['optionid'],'goodsid'=>$itemv['id']),'specs');				
				$selectspecs = str_array($specs,'_');
				if($selectspecs){
						foreach($selectspecs as $k=>&$spid){
							$title=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spid,'uniacid'=>$_SESSION['uniacid']),'title');
							$selespename .=$title.'，';
						}
				}
				//默认已选参数：
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$cartnum=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$itemv['options']['id'],'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
				$itemv['post'][$itemv['options']['id']]=array('num'=>$cartnum?:0,'title'=>$selespename);
			}
			if($_SESSION['is_tomedia']){
				$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
				$itemv['video']=$itemv['video']?tomedia($itemv['video']):'';
			}
			if($_RQ['wxappjiaoyi']){
				$itemv['id']=$itemv['api_id'];
			}
			$itemv['api_data']=$itemv['api_data']?json_decode($itemv['api_data'],true):array();
		}
	}
	exi($list);
}
?>